﻿global using System.Collections;

global using WXWorkSvc.Common.Models;

namespace WXWorkSvc.Common.TagHelpers;
/// <summary>
/// Index=>List 多个链接，onclick支持
/// </summary>
[HtmlTargetElement("desk-list", TagStructure = TagStructure.WithoutEndTag)]
public class DeskListTagHelper : TagHelper
{

    #region Properties
    private readonly IHtmlGenerator _HtmlGenerator;
    private readonly Dictionary<string, object> _ItemValues = new();
    public DeskListTagHelper(IHtmlGenerator generator)
    {
        _HtmlGenerator = generator;
    }

    [HtmlAttributeName("asp-items")]
    public ModelExpression Items { get; set; } = default!;
    [HtmlAttributeName("asp-need-rowno")]
    public bool IncludeRowNo { get; set; } = true;
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-for-")]
    public Dictionary<string, ModelExpression> TDFors { get; set; } = new();

    [HtmlAttributeName(DictionaryAttributePrefix = "asp-link-text-")]
    public Dictionary<string, string> PageLinkTexts { get; set; } = new();
    /// <summary>
    /// 若为空，则onclick(asp-route-values..)
    /// </summary>
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-page-")]
    public Dictionary<string, string> PageNames { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-fun-")]
    public Dictionary<string, string> FunctionNames { get; set; } = new();
    /// <summary>
    /// 常量参数
    /// </summary>
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-const-")]
    public Dictionary<string, object> ConstValues { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-route-")]
    public Dictionary<string, ModelExpression> RouteValues { get; set; } = new();

    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; } = default!;
    #endregion

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "table";
        output.TagMode = TagMode.StartTagAndEndTag;
        output.Attributes.Add("class", "table table-striped table-hover");

        TagBuilder tagHead = new TagBuilder("thead");
        TagBuilder tagHeadRow = new TagBuilder("tr");
        if (IncludeRowNo)
            tagHeadRow.InnerHtml.AppendHtml("<th>#</th>");
        foreach (var f in TDFors)
        {
            var tagSpan = new TagBuilder("span");
            tagSpan.AddHtmlTooltip(f.Value.Metadata);

            var tagTH = new TagBuilder("th");
            tagTH.InnerHtml.SetHtmlContent(tagSpan);

            tagHeadRow.InnerHtml.AppendHtml(tagTH);
        }
        if (PageLinkTexts.Count > 0)
            tagHeadRow.InnerHtml.AppendHtml("<th></th>");
        tagHead.InnerHtml.AppendHtml(tagHeadRow);
        output.Content.SetHtmlContent(tagHead);

        var tagBody = new TagBuilder("tbody");


        int rowNo = 1;
        foreach (var item in (IList)Items.Model)
        {
            _ItemValues.Clear();
            var itemModelExplorer = Items.ModelExplorer.GetExplorerForModel(item);

            var tagRow = new TagBuilder("tr");
            if (IncludeRowNo)
                tagRow.InnerHtml.AppendHtml($"<td>{rowNo++}</td>");
            foreach (var f in TDFors)
            {
                tagRow.InnerHtml.AppendHtml($"<td>{itemModelExplorer.GetModelValue(_ItemValues, f.Value.Metadata.Name!)}</td>");
            }
            if (PageLinkTexts.Count > 0)
            {
                var count = 0;
                var tagColumn = new TagBuilder("td");
                foreach (var kv in PageLinkTexts)
                {
                    var routeValues = itemModelExplorer.GetRouteValues(_ItemValues, ConstValues, RouteValues, kv.Key);

                    if (PageNames.TryGetValue(kv.Key, out var pageName))
                    {
                        tagColumn.InnerHtml.AppendHtml(_HtmlGenerator.GeneratePageLink(ViewContext, kv.Value, pageName, null, null, null, null, routeValues, null));
                    }
                    else if (FunctionNames.TryGetValue(kv.Key, out var functionName))
                    {
                        var linkTagBuilder = new TagBuilder("a");
                        linkTagBuilder.InnerHtml.SetContent(kv.Value);
                        linkTagBuilder.Attributes.Add("href", "javascript:;");
                        linkTagBuilder.Attributes.Add("onclick", routeValues.ToFunction(functionName));
                        tagColumn.InnerHtml.AppendHtml(linkTagBuilder);
                    }
                    count++;
                    if (count < PageLinkTexts.Count)
                        tagColumn.InnerHtml.Append(" | ");
                }
                tagRow.InnerHtml.AppendHtml(tagColumn);
            }
            tagBody.InnerHtml.AppendHtml(tagRow);

        }
        output.Content.AppendHtml(tagBody);
    }


}
